<?php

namespace yak\layui\models;

/**
 * This is the model class for table "{{%menu}}".
 *
 * @property int $id
 * @property int|null $pid 上级菜单
 * @property int $pos 菜单位置
 * @property string $title
 * @property string $href
 * @property string $language
 * @property string|null $icon
 * @property int|null $active
 * @property int $sort 排序
 * @property int $status 0未激活，1激活中
 * @property int $created_at
 * @property int $updated_at
 */
class Menu extends Yak
{
    const POS_BODY_LEFT = 0;
    const POS_TOP_LEFT = 1;
    const POS_TOP_RIGHT = 2;

    const TYPE_NORMAL = 0;
    const TYPE_JUST_ICON = 1;
    const TYPE_JUST_TEXT = 2;
    const TYPE_SEARCH = 3;
    const TYPE_PIC_MIXED = 4;
    const TYPE_CUSTOMER = 10;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%menus}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['pid', 'pos', 'active', 'sort', 'status', 'type'], 'integer'],
            [['title'], 'required'],
            [['title', 'language', 'icon'], 'string', 'max' => 255],
            [['href', 'event'], 'string', 'max' => 128],
            [['sort', 'status', 'pos', 'active'], 'default', 'value' => 0]
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => '记录ID',
            'pid' => '父菜单',
            'pos' => '位置',
            'title' => '名称',
            'href' => '链接',
            'language' => '语言',
            'icon' => '图标',
            'type' => '类型',
            'event' => '事件',
            'active' => '激活',
            'sort' => '排序',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    public static function menuData($pos = 0)
    {
        $dependency = new \yii\caching\DbDependency(['db' => self::getDbSign(), 'sql' => 'SELECT MAX(updated_at) FROM ' . self::tableName()]);
        return self::find()->cache(3600, $dependency)->indexBy('id')->where(['pos' => $pos])->orderBy('sort DESC')->asArray()->all();
    }

    public function posLabels()
    {
        return [
            self::POS_BODY_LEFT => '侧边栏',
            self::POS_TOP_LEFT => '顶部左侧',
            self::POS_TOP_RIGHT => '顶部右侧'
        ];
    }

    public function typeLabels()
    {
        return [
            self::TYPE_NORMAL => '普通',
            self::TYPE_JUST_ICON => '图标',
            self::TYPE_JUST_TEXT => '文字',
            self::TYPE_SEARCH => '搜索框',
            self::TYPE_PIC_MIXED => '图文',
            self::TYPE_CUSTOMER => '自定义'
        ];
    }
}
